/*
 * jiyan1
 */
package com.jiyan.sample.ctrl;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;
import springfox.documentation.annotations.ApiIgnore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import com.rzhkj.core.tools.redis.RedisUtils;
import com.rzhkj.core.tools.StringTools;
import com.rzhkj.core.entity.BeanRet;
import com.rzhkj.core.entity.Page;
import com.jiyan.core.base.BaseCtrl;
import com.jiyan.sample.facade.SampleSV;
import com.jiyan.sample.entity.Sample;



/**
 * 样品
 *
 * @author jiyan
 */
@Controller
@RequestMapping("/sample")
@Api(value = "样品控制器", description = "样品控制器")
public class SampleCtrl extends BaseCtrl {
 private final static Logger logger = LoggerFactory.getLogger(SampleCtrl.class);

    @Resource
    protected RedisTemplate<String, Object> redisTemplate;
    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private SampleSV sampleSV;



    /**
     * 查询样品详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询样品详情信息", notes = "查询样品详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    })
    @GetMapping(value = "/loadById")
    @ResponseBody
    public BeanRet loadById(@PathVariable java.lang.Long id) {
        if(id == null){
          return BeanRet.create();
        }
        
        Sample sample = sampleSV.load(id);
        logger.info(JSON.toJSONString(sample));
        return BeanRet.create(true, "查询详情信息", sample);
    }


    /**
     * 查询样品详情信息
     *
     * @param code 样品编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询样品详情信息", notes = "查询样品详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "样品编码", paramType = "query")
    })
    @GetMapping(value = "/loadByCode")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable java.lang.String code) {
        if(StringTools.isEmpty(code)){
          return BeanRet.create();
        }
        
        Sample sample = sampleSV.loadByCode(code);
        logger.info(JSON.toJSONString(sample));
        return BeanRet.create(true, "查询详情信息", sample);
    }



    /**
     * 查询样品信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询样品信息集合", notes = "查询样品信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "样品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "样品名称", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Sample sample,@ApiIgnore Page<Sample> page) {
        if (page == null) {
                return BeanRet.create(false, "分页对象不能为空");
        }
        page.setParams(JSON.parseObject(JSON.toJSONString(sample)));
        page = sampleSV.getList(page);
        return BeanRet.create(true, "", page);
    }

    /**
     * 创建样品
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建样品", notes = "创建样品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "样品编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "样品名称", paramType = "query", required = true)
    })
    @PostMapping("/save")
    @ResponseBody
    public BeanRet save(@ApiIgnore Sample sample) {
        sampleSV.save(sample);
        return BeanRet.create(true, "保存成功");
    }


    /**
     * 修改样品
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改样品", notes = "修改样品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "样品编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "样品名称", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Sample sample) {
        sampleSV.update(sample);
        return BeanRet.create(true, "修改成功");
    }

    /**
     * 删除样品
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除样品", notes = "删除样品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "样品编码", paramType = "query", required = true)
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(java.lang.Long id,java.lang.String code) {
        if(id==null){
          return BeanRet.create(false,"id不能为空");
        }
        if(code==null){
          return BeanRet.create(false,"样品编码不能为空");
        }
        sampleSV.delete(id,code);
        return BeanRet.create(true, "删除Sample成功");
    }

}
